﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing.Imaging;

namespace Uebung
{
    public partial class UI : Form
    {
        BildEditor edi;
        private bool _selecting;
        private Rectangle _selection;

        public UI()
        {
            InitializeComponent();
            panel1.Width = Width-30;
            panel1.Left = 5;
            edi = new BildEditor(this);
        }
        

        private void öffnenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            return;
            
            string path = fbd.SelectedPath;
            panel1.Controls.Clear();
            ArrayList imgs = edi.imagestoarray(path);

            
            int x = 0, y = 0, ps = 100, space = 10;
            
            foreach (Bild bild in imgs)
            {
                PictureBox pb = new PictureBox()
                {
                    Location = new Point(x * 110, y * 110),
                    Height = 100,
                    Width = 100,
                    Image = bild.bmp,
                };

                pb.SizeMode = PictureBoxSizeMode.Zoom;


                ++x;
                if ((x * (ps+space)) > panel1.Width - (ps+space))
                {
                    x = 0;
                    ++y;
                }
                pb.Click += pb_Click;
                panel1.Controls.Add(pb);
            }
            panel1.AutoScroll = true;
        }

        void pb_Click(object sender, EventArgs e)
        {
            this.panel1.AutoScroll = true;
            if (edi.activeBild == null)
            {
                pictureBox1.Image = (sender as PictureBox).Image;
                //pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                //pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
                //pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                
                edi.activeBild = edi.imgs[(sender as PictureBox).TabIndex] as Bild;
                edi.activeBild.origin = new Point((int)(pictureBox1.Width - pictureBox1.Image.Width) / -2,
                    (int)(pictureBox1.Height - pictureBox1.Image.Height) / -2);
                Debug.Print("origin:" + edi.activeBild.origin.ToString());                
            }
            else if (!edi.activeBild.aenderung && pictureBox1 != null)
            {
                pictureBox1.Image = (sender as PictureBox).Image;
            }


            else if (!edi.activeBild.saved && edi.activeBild.aenderung)
            {
                DialogResult dlgrslt;

                dlgrslt = MessageBox.Show("Speichern?", "Speicherfehler",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

                if (dlgrslt == DialogResult.OK)
                {
                    string vorbeendenspeichern = @"c:\\";
                    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                    saveFileDialog1.Filter = "jpg Files (*.jpg)|*.jpg|bmp Files (*.bmp)|*.bmp";
                    saveFileDialog1.InitialDirectory = vorbeendenspeichern;

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        vorbeendenspeichern = saveFileDialog1.FileName;
                        try
                        {
                            if (saveFileDialog1.Filter.Contains("jpg"))
                            {
                                pictureBox1.Image.Save(vorbeendenspeichern, ImageFormat.Jpeg);
                            }

                            else
                            {
                                pictureBox1.Image.Save(vorbeendenspeichern, ImageFormat.Bmp);
                            }
                            edi.activeBild.saved = true;
                            edi.activeBild.aenderung = false;
                        }
                        catch (Exception SaveException)
                        {
                            MessageBox.Show("Sie haben einen falschen Pfad gewählt!\n\n\nCode:\n" + Convert.ToString(SaveException), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Debug.Print("edi.activeBild.zuschneiden" + (edi.activeBild.zuschneiden == true ? "yes" : "no"));
            if (edi.activeBild.zuschneiden == true)
            {
                // Starting point of the selection:
                if (e.Button == MouseButtons.Left)
                {
                    _selecting = true;
                    _selection = new Rectangle(new Point(e.X, e.Y), new Size());
                }

            }
        }
        //---------------------------------------------------------------------
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

                // Update the actual size of the selection:
                if (_selecting)
                {
                    _selection.Width = e.X - _selection.X;
                    _selection.Height = e.Y - _selection.Y;

                    // Redraw the picturebox:
                    pictureBox1.Refresh();
                }

            
        }
        
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (edi.activeBild.zuschneiden == true)
            {
                if (!edi.ValidateSelection(_selection))
                    return;

                if (e.Button == MouseButtons.Left && _selecting)
                {
                    
                    Bitmap bmp = edi.Crop(_selection);


                    pictureBox1.Image = bmp;

                    edi.activeBild.origin = new Point((int)(pictureBox1.Width - pictureBox1.Image.Width) / -2,
                                        (int)(pictureBox1.Height - pictureBox1.Image.Height) / -2);
                    _selecting = false;
                }
            }
            
        }
        

        private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
        {

                if (_selecting)
                {
                    //zeichnet das grüne rechteck
                    Pen pen = Pens.GreenYellow;
                    e.Graphics.DrawRectangle(pen, _selection);
                }

        }

        //---------------------------------------------------------------------



        private void sichernToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if(edi.activeBild == null)
            {
                return;
            }

            string Pfad = @"c:\\";
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "jpg Files (*.jpg)|*.jpg|bmp Files (*.bmp)|*.bmp";
            saveFileDialog1.InitialDirectory = Pfad;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Pfad = saveFileDialog1.FileName;
                try
                {
                    if (saveFileDialog1.Filter.Contains("jpg"))
                    {
                        pictureBox1.Image.Save(Pfad, ImageFormat.Jpeg);
                    }

                    else
                    {
                        pictureBox1.Image.Save(Pfad, ImageFormat.Bmp);
                    }
                    edi.activeBild.saved = true;
                    edi.activeBild.aenderung = false;
                }
                catch (Exception SaveException)
                {
                    MessageBox.Show("Sie haben einen falschen Pfad gewählt!\n\n\nCode:\n" + Convert.ToString(SaveException), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

        }

        private void zuschneidenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                edi.activeBild.zuschneiden = true;
                if (edi.activeBild.zuschneiden)
                {
                    edi.activeBild.aenderung = true;
                }
                edi.activeBild.saved = false;
            }
        }




 



    }
}
